import pymysql


def main():
    # 创建连接对象
    conn = pymysql.connect(host='120.24.84.91', port=3306,
                           user='root', password='123456',
                           db='school', charset='utf8',
                           cursorclass=pymysql.cursors.DictCursor)
    # name=input('请输入名字：')
    # cid=input('请输入编号：')

    try:
        # 获得游标对象
        with conn.cursor() as cursor:  # 上下文语法，执行完会自动关
            # 执行SQL命令得到结果
            cursor.execute('select colid as id, colname as name,website as web from tb_colleage ')
            # print(cursor.fetchall())
            for row in cursor.fetchall():
                print(row['id'],end='\t')
                print(row['name'],end='\t')
                print(row['web'])
                # print('*' * 20)
            # 操作成功执行提交
            conn.commit()
    except pymysql.MySQLError as error:
        print(error)
    finally:
        # 关闭连接释放资源
        conn.close()

    # print(conn)


if __name__ == '__main__':
    main()
